package cn.com.tuns.assess.qsb.frame.util;

import android.graphics.*;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

/**
 * @author：luck
 * @date：2020-01-15 18:22
 * @describe：BitmapUtils
 */
public class BitmapUtils {
    /**
     * 旋转Bitmap
     *
     * @param bitmap
     * @param angle
     * @return
     */
    public static Bitmap rotatingImage(Bitmap bitmap, int angle) {
        Matrix matrix = new Matrix();

        matrix.postRotate(angle);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    public static Bitmap createBitmap(Bitmap bitmap) {
        Bitmap tmp=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(tmp);
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(bitmap,0,0,new Paint());
        return tmp;
    }

    public static Bitmap createBitmap(Bitmap bitmap,int width,int height) {
        Bitmap tmp=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(tmp);
        canvas.drawColor(Color.TRANSPARENT);
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        canvas.drawBitmap(bitmap,null,new Rect(0,0,width,height),paint);
        return tmp;
    }

    /**
     * 判断拍照 图片是否旋转
     *
     * @param degree
     * @param path
     */
    public static void rotateImage(int degree, String path) {
        if (degree > 0) {
            try {
                // 针对相片有旋转问题的处理方式
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inSampleSize = 2;
                File file = new File(path);
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
                bitmap = rotatingImage(bitmap, degree);
                if (bitmap != null) {
                    saveBitmapFile(bitmap, file);
                    bitmap.recycle();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveBitmapFile(Bitmap bitmap, File file) {
        saveBitmapFile(bitmap,file,100);
    }
    /**
     * 保存Bitmap至本地
     *
     * @param bitmap
     * @param file
     */
    public static void saveBitmapFile(Bitmap bitmap, File file,int quality) {
        BufferedOutputStream bos =null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
            bos.flush();
            bos.close();
            
            bos=null;
        } catch (Exception e) {
            e.printStackTrace();

        }
        finally {
            if(bos!=null)
            {
                try {
                    bos.close();
                    bos=null;
                }catch (Exception e)
                {

                }

            }
        }
    }

    /**
     * 获取旋转角度
     *
     * @param orientation
     * @return
     */
    public static int getRotationAngle(int orientation) {
        switch (orientation) {
            case 1:
                return 0;
            case 3:
                return 180;
            case 6:
                return 90;
            case 8:
                return 270;
        }
        return 0;
    }

    /**
     * 获取图片缩略图的方法
     *
     * @param path      图片路径
     * @param maxLength 缩略后图片宽高最大值 若参数小于0 则用默认值800
     * @return
     */
    public static Bitmap getLittleBitmap(String path, int maxLength) {
        if (maxLength <= 0)
            maxLength = 800;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        // 当opts不为null时，但decodeFile返回空，不为图片分配内存，只获取图片的大小，并保存在opts的outWidth和outHeight
        BitmapFactory.decodeFile(path, opts);
        int srcWidth = opts.outWidth;
        int srcHeight = opts.outHeight;
        int destWidth = 0;
        int destHeight = 0;
        double ratio = 0.0; // 缩放的比例
        // tvInfo.setText("Width:" + srcWidth + " Height:" + srcHeight);
        // //tvInfo是一个TextView用于显示图片的尺寸信息
        // Log.e("Bitmap final size", srcWidth+" "+srcHeight);
        // 若图片本来就很小
        if (srcWidth < maxLength && srcHeight < maxLength) {
            Bitmap destBm = BitmapFactory.decodeFile(path);
            return destBm;
        }
        // 按比例计算缩放后的图片大小，maxLength是长或宽允许的最大长度
        if (srcWidth > srcHeight) {
            ratio = (float) srcWidth / maxLength;
            destWidth = maxLength;
            destHeight = (int) (srcHeight / ratio);
        } else {
            ratio = (float) srcHeight / maxLength;
            destHeight = maxLength;
            destWidth = (int) (srcWidth / ratio);
        }
        // 对图片进行压缩，是在读取的过程中进行压缩，而不是把图片读进了内存再进行压缩
        // BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 缩放的比例，缩放是很难按准备的比例进行缩放的，目前我只发现只能通过inSampleSize来进行缩放，其值表明缩放的倍数，SDK中建议其值是2的指数值
        opts.inSampleSize = (int) ratio + 1;
        // inJustDecodeBounds设为false表示把图片读进内存中
        opts.inJustDecodeBounds = false;
        // 设置大小，这个一般是不准确的，是以inSampleSize的为准，但是如果不设置却不能缩放
        opts.outHeight = destHeight;
        opts.outWidth = destWidth;
        // 添加尺寸信息，
        // tvInfo.append("\nWidth:" + newOpts.outWidth + " Height:" +
        // newOpts.outHeight);
        // 获取缩放后图片
        // Log.e("Bitmap end size", newOpts.outWidth+" "+newOpts.outHeight);
        Bitmap destBm = BitmapFactory.decodeFile(path, opts);
        if (destBm == null) {
            return null;
        }

        return destBm;
    }

    public static boolean isBitmap(String path){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        if (options.outWidth != -1 && options.outHeight != -1) {
            return true;
        }
        else {
            return false;
        }
    }

    public static Bitmap setAlpha(Bitmap sourceImg, int number) {
        int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
        sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight());
        // 获得图片的ARGB值
//        number = number * 255 / 100;
        for (int i = 0; i < argb.length; i++)
        {
            argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);
            // 修改最高2位的值
        }
        sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Bitmap.Config.ARGB_8888);
        return sourceImg;
    }
}
